package com.jxs.android.ges;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.util.KeyboardUtil;
import com.jxs.android.util.StringUtils;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.ClickAbleText;
import com.jxs.android.weiget.RefreshListView;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.ges.R;

/**
 * @Author jiangxs
 * @Des
 */
public class Search_Of_User extends LoadingMoreAbstractList implements OnClickListener {

	private final int noticePageSize = 50;
	private NoticeListInfoAdapter adapter;
	private View bu_search_user_list_head;
	private EditText search_phone, search_user;
	private ImageButton search_phone_btn, search_user_btn;
	private TextView top_total_bar;
	protected ImageView backtop;

	@Override
	public int getLayout() {
		return R.layout.bu_search_user;
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindSpinnerBtn();
		topBarReturn(topBar);
		topBar.setTitle(R.string.search_user);
	}

	@Override
	protected void addListHeadView(RefreshListView refreshListView) {
		super.addListHeadView(refreshListView);
		top_total_bar = (TextView) findViewById(R.id.top_total_bar);
		top_total_bar.setVisibility(View.GONE);
		bu_search_user_list_head = LayoutInflater.from(this).inflate(R.layout.bu_search_list_header, null);

		search_phone = (EditText) bu_search_user_list_head.findViewById(R.id.bu_search_phone);
		search_user = (EditText) bu_search_user_list_head.findViewById(R.id.bu_search_user);

		search_user_btn = (ImageButton) bu_search_user_list_head.findViewById(R.id.bu_search_user_btn);
		search_phone_btn = (ImageButton) bu_search_user_list_head.findViewById(R.id.bu_search_phone_btn);

		search_user_btn.setOnClickListener(this);
		search_phone_btn.setOnClickListener(this);

		refreshListView.addHeaderView(bu_search_user_list_head, null, false);
		backTop();
	}

	@Override
	protected boolean isOnCreatLoading() {
		return false;
	}

	private Which which = Which.USER_SEARCH_NAME;
	private String editValue = "";

	@Override
	public void onClick(View v) {
		currentPage = 1;
		if (v.getId() == search_user_btn.getId()) {
			which = Which.USER_SEARCH_NAME;
			editValue = search_user.getEditableText().toString();
			if (StringUtils.isEmpety(editValue)) {
				errorMsgPop(R.string.bu_search_user_name);
				return;
			}
			loadingData(currentPage, true);
		} else if (v.getId() == search_phone_btn.getId()) {
			which = Which.USER_SEARCH_PHONE;
			editValue = search_phone.getEditableText().toString();
			if (StringUtils.isEmpety(editValue)) {
				errorMsgPop(R.string.bu_search_user_phone);
				return;
			}
			loadingData(currentPage, true);
		}
	}

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		if (StringUtils.isEmpety(editValue)) {
			errorMsgPop(R.string.bu_search_user_null);
			refreshListView.onRefreshComplete();
			return;
		}

		if (KeyboardUtil.isImeShow(this)) {
			KeyboardUtil.hideIme(this);
		}

		PAGE_SIZE = noticePageSize;
		getWebServiceData(GesApplication.getUserInfo().getTicket(), which, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, editValue, PAGE_SIZE, currentPage);
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new NoticeListInfoAdapter(this);
		return adapter;
	}

	@Override
	public List<SearchUserInfo> paserData(SoapObject retMsgObject, Object... params) {
		List<SearchUserInfo> searchUserInfos = new ArrayList<SearchUserInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				SearchUserInfo searchUserInfo = new SearchUserInfo();

				searchUserInfo.acc_nbr = getValue(taskBeanObject, "acc_nbr");// 18919889828
				searchUserInfo.acctarpu = getValue(taskBeanObject, "acctarpu");// 账户ARPU:109.98
				searchUserInfo.chnl_manager_name = getValue(taskBeanObject, "chnl_manager_name");// 用户ARPU:47.48
				searchUserInfo.cur_mon_flow = getValue(taskBeanObject, "cur_mon_flow");// 本月流量:13.73M
				searchUserInfo.cust_id = getValue(taskBeanObject, "cust_id");// 931021085788
				searchUserInfo.cust_sub_brand_name = getValue(taskBeanObject, "cust_sub_brand_name");// T5校园卡
				searchUserInfo.join_acct_serv_num = getValue(taskBeanObject, "join_acct_serv_num");// 合帐用户数:4
				searchUserInfo.join_date = getValue(taskBeanObject, "join_date");// 入网日期:2009-09-24
				searchUserInfo.last_mon_flow = getValue(taskBeanObject, "last_mon_flow");// 上月流量:18.77M
				searchUserInfo.latn_name = getValue(taskBeanObject, "latn_name");// 兰州;
				searchUserInfo.pre_pay_bal = getValue(taskBeanObject, "pre_pay_bal");// 帐户余额:1255.17
				searchUserInfo.serv_cust_type_name = getValue(taskBeanObject, "serv_cust_type_name");// 公众
				searchUserInfo.serv_id = getValue(taskBeanObject, "serv_id");// 931022677978;
				searchUserInfo.serv_name = getValue(taskBeanObject, "serv_name");// 王锡勇
				searchUserInfo.serv_state_name = getValue(taskBeanObject, "serv_state_name");// 正常
				searchUserInfo.serv_trait_name = getValue(taskBeanObject, "serv_trait_name");// 中值高预存款;

				searchUserInfos.add(searchUserInfo);
			}
		}
		return searchUserInfos;
	}

	@Override
	public void updateList(Message msg) {

		if (null != msg.getData()) {
			if (!StringUtils.isEmpety(msg.getData().getString("totalRows"))) {
				top_total_bar.setVisibility(View.VISIBLE);
				top_total_bar.setText("记录总数:【" + msg.getData().getString("totalRows") + " 】条");
			}

		}

		@SuppressWarnings("unchecked")
		List<SearchUserInfo> tems = ((List<SearchUserInfo>) msg.obj);
		refreshListView.onRefreshComplete();
		empytText.setVisibility(View.GONE);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				loadMoreButton.setText("数据已全部获取完毕");
				loadMoreButton.setEnabled(false);
			}
		}
		adapter.addSearchUserInfos(tems);
		adapter.notifyDataSetChanged();
		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			currentPage++;
		}
		if (adapter.getCount() > 5) {
			backtop.setVisibility(View.VISIBLE);
		} else {
			backtop.setVisibility(View.INVISIBLE);
		}
	}

	private class NoticeListInfoAdapter extends BaseAdapter {
		private List<SearchUserInfo> searchUserInfos = new ArrayList<SearchUserInfo>();
		private Context context;

		public void addSearchUserInfos(List<SearchUserInfo> searchUserInfos) {
			if (currentPage == 1) {
				this.searchUserInfos.clear();
			}
			this.searchUserInfos.addAll(searchUserInfos);
		}

		public NoticeListInfoAdapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return searchUserInfos.size();
		}

		public SearchUserInfo getItem(int position) {
			return searchUserInfos.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final SearchUserInfo searchUserInfo = searchUserInfos.get(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.bu_list_search_rst_item, null);
				holder = new ViewHolder();
				holder.position = (TextView) convertView.findViewById(R.id.position);// 18919889828
				holder.acc_nbr = (ClickAbleText) convertView.findViewById(R.id.acc_nbr);// 18919889828
				holder.acc_nbr.setClickAble(true);
				holder.acctarpu = (TextView) convertView.findViewById(R.id.acctarpu);// 账户ARPU:109.98
				holder.chnl_manager_name = (TextView) convertView.findViewById(R.id.chnl_manager_name);// 用户ARPU:47.48
				holder.cur_mon_flow = (TextView) convertView.findViewById(R.id.cur_mon_flow);// 本月流量:13.73M
				holder.cust_sub_brand_name = (TextView) convertView.findViewById(R.id.cust_sub_brand_name);// T5校园卡
				holder.join_acct_serv_num = (TextView) convertView.findViewById(R.id.join_acct_serv_num);// 合帐用户数:4
				holder.join_date = (TextView) convertView.findViewById(R.id.join_date);// 入网日期:2009-09-24
				holder.last_mon_flow = (TextView) convertView.findViewById(R.id.last_mon_flow);// 上月流量:18.77M
				holder.latn_name = (TextView) convertView.findViewById(R.id.latn_name);// 兰州=(TextView)
																						// convertView.findViewById(R.id.item_title);
				holder.pre_pay_bal = (TextView) convertView.findViewById(R.id.pre_pay_bal);// 帐户余额:1255.17
				holder.serv_cust_type_name = (TextView) convertView.findViewById(R.id.serv_cust_type_name);// 公众
				holder.serv_name = (TextView) convertView.findViewById(R.id.serv_name);// 王锡勇
				holder.serv_state_name = (TextView) convertView.findViewById(R.id.serv_state_name);// 正常
				holder.item_arrow = (ImageView) convertView.findViewById(R.id.item_arrow);// 正常
				// 中值高预存款;
				// holder.serv_trait_name = (TextView)
				// convertView.findViewById(R.id.serv_trait_name);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.position.setText("No." + (position + 1));// 18919889828
			holder.acc_nbr.setText(searchUserInfo.acc_nbr);// 18919889828
			holder.acctarpu.setText(searchUserInfo.acctarpu);// 账户ARPU:109.98
			holder.chnl_manager_name.setText(searchUserInfo.chnl_manager_name);// 用户ARPU:47.48
			holder.cur_mon_flow.setText(searchUserInfo.cur_mon_flow);// 本月流量:13.73M
			// holder. cust_id.setText(searchUserInfo.cust_id);// 931021085788
			holder.cust_sub_brand_name.setText(searchUserInfo.cust_sub_brand_name);// T5校园卡
			holder.join_acct_serv_num.setText(searchUserInfo.join_acct_serv_num);// 合帐用户数:4
			holder.join_date.setText(searchUserInfo.join_date);// 入网日期:2009-09-24
			holder.last_mon_flow.setText(searchUserInfo.last_mon_flow);// 上月流量:18.77M
			holder.latn_name.setText(searchUserInfo.latn_name);// 兰州.setText(searchUserInfo.item_title);
			holder.pre_pay_bal.setText(searchUserInfo.pre_pay_bal);// 帐户余额:1255.17
			holder.serv_cust_type_name.setText(searchUserInfo.serv_cust_type_name);// 公众
			holder.serv_name.setText(searchUserInfo.serv_name);// 王锡勇
			holder.serv_state_name.setText(searchUserInfo.serv_state_name);// 正常
			if (null != holder.serv_trait_name)
				holder.serv_trait_name.setText(searchUserInfo.serv_trait_name);

			View.OnClickListener listener = new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onclickView(searchUserInfo);
				}
			};

			holder.acc_nbr.setOnClickListener(listener);
			holder.item_arrow.setOnClickListener(listener);
			// convertView.setOnClickListener(listener);
			return convertView;
		}

		private void onclickView(SearchUserInfo noticeListInfo) {
			Intent intent = new Intent(context, Search_Result_HomePages.class);
			intent.putExtra(Search_Result_HomePages.TAG_SERV_ID, noticeListInfo.serv_id);
			intent.putExtra(Search_Result_HomePages.TAG_CUST_ID, noticeListInfo.cust_id);
			intent.putExtra(Search_Result_HomePages.TAG_CUST_NBR, noticeListInfo.acc_nbr);
			startActivity(intent);
		}

		class ViewHolder {
			TextView position;// position
			ClickAbleText acc_nbr;// 18919889828
			TextView acctarpu;// 账户ARPU:109.98
			TextView chnl_manager_name;// 用户ARPU:47.48
			TextView cur_mon_flow;// 本月流量:13.73M
			TextView cust_sub_brand_name;// T5校园卡
			TextView join_acct_serv_num;// 合帐用户数:4
			TextView join_date;// 入网日期:2009-09-24
			TextView last_mon_flow;// 上月流量:18.77M
			TextView latn_name;// 兰州;
			TextView pre_pay_bal;// 帐户余额:1255.17
			TextView serv_cust_type_name;// 公众
			TextView serv_name;// 王锡勇
			TextView serv_state_name;// 正常
			TextView serv_trait_name;// 中值高预存款;
			ImageView item_arrow;// 中值高预存款;

		}
	}

	@Override
	public String getMethod() {

		if (which.ordinal() == Which.USER_SEARCH_NAME.ordinal()) {
			return "getUserNameSearchNew";
		} else if (which.ordinal() == Which.USER_SEARCH_PHONE.ordinal()) {
			return "getAccNbrSearchNew";
		}
		return "";

	}

	class SearchUserInfo {

		String acc_nbr = "";// 18919889828
		String acctarpu = "";// 账户ARPU:109.98
		String chnl_manager_name = "";// 用户ARPU:47.48
		String cur_mon_flow = "";// 本月流量:13.73M
		String cust_id = "";// 931021085788
		String cust_sub_brand_name = "";// T5校园卡
		String join_acct_serv_num = "";// 合帐用户数:4
		String join_date = "";// 入网日期:2009-09-24
		String last_mon_flow = "";// 上月流量:18.77M
		String latn_name = "";// 兰州;
		String pre_pay_bal = "";// 帐户余额:1255.17
		String serv_cust_type_name = "";// 公众
		String serv_id = "";// 931022677978;
		String serv_name = "";// 王锡勇
		String serv_state_name = "";// 正常
		String serv_trait_name = "";// 中值高预存款;

	}

	private void backTop() {
		backtop = (ImageView) findViewById(R.id.back_top);
		backtop.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View paramView) {
				refreshListView.setSelection(1);
			}
		});
	}

	void laucherListScorll() {
		refreshListView.setOnScrollListener(new AbsListView.OnScrollListener() {

			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				if (firstVisibleItem > 5) {
					backtop.setVisibility(View.VISIBLE);
				} else {
					backtop.setVisibility(View.INVISIBLE);
				}
			}
		});
	}

}
